package com.hoperun.service.impl;

import java.io.IOException;
import java.util.HashMap;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.hoperun.service.IYsSDKSV;
import com.hoperun.utils.HttpUtils;

@Service("ysSDKSV")
public class YsSDKSVImpl implements IYsSDKSV {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(YsSDKSVImpl.class);

	@Value("#{config['ys_add_user']}")
	private String ys_add_user;

	@Value("#{config['ys_key']}")
	private String ys_key;

	@Value("#{config['ys_batch_add_user']}")
	private String ys_batch_add_user;

	@Value("#{config['ys_send_user_msg']}")
	private String ys_send_user_msg;

	/**
	 * Add user to YunShiHudong system
	 * 
	 * @param uname
	 * @param pwd
	 * @return
	 * @throws IOException
	 */
	@Override
	public Integer addUser(String uname, String pwd) throws Exception {

		NameValuePair[] pairs = new NameValuePair[3];
		NameValuePair pair0 = new NameValuePair("key", "" + ys_key);
		NameValuePair pair1 = new NameValuePair("username", "" + uname);
		NameValuePair pair2 = new NameValuePair("pwd", "" + pwd);

		pairs[0] = pair0;
		pairs[1] = pair1;
		pairs[2] = pair2;

		String result = HttpUtils.POST(ys_add_user, pairs);

		if (StringUtils.isNotBlank(result)) {
			String[] rst = result.split("=");
			return Integer.valueOf(rst[1]);
		}

		return null;
	}

	@Override
	public HashMap<String, String> batchAddUser(String user_info_json)
			throws Exception {

		NameValuePair[] pairs = new NameValuePair[2];
		NameValuePair pair0 = new NameValuePair("key", ys_key);
		NameValuePair pair1 = new NameValuePair("userinfo", user_info_json);

		pairs[0] = pair0;
		pairs[1] = pair1;

		String result = HttpUtils.POST(ys_batch_add_user, pairs);
		LOGGER.debug("post result is {}", result);
		// HashMap<String, String> result = httpRequest(wholeURL, params,
		// "POST");
		//
		// if (null == result)
		// return ret;
		// ret = Integer.parseInt(result.get("ret_code"));
		// return ret;
		return null;
	}

	/**
	 * Send user message to other device or app client
	 * 
	 * @param srcUname
	 * @param dstUname
	 * @param content
	 * @return
	 * @throws IOExceptions
	 */
	@Override
	public String sendMessage(String srcUname, String dstUname, String content)
			throws Exception {

		NameValuePair[] pairs = new NameValuePair[4];
		NameValuePair pair0 = new NameValuePair("key", ys_key);
		NameValuePair pair1 = new NameValuePair("username", srcUname);
		NameValuePair pair2 = new NameValuePair("dst", dstUname);
		NameValuePair pair3 = new NameValuePair("msg", content);

		pairs[0] = pair0;
		pairs[1] = pair1;
		pairs[2] = pair2;
		pairs[3] = pair3;

		String result = HttpUtils.POST(ys_send_user_msg, pairs);
		LOGGER.debug("post result is {}", result);
		// HashMap<String, String> result = httpRequest(wholeURL, params,
		// "POST");
		//
		// if (null == result)
		// return ret;
		// ret = Integer.parseInt(result.get("ret_code"));
		// return ret;
		return result;
	}

}
